const {
    getProject,
    updateProject,
    createProject,
    destroyProject,
    changeStatusProject,
    getProjectUp
} = require('../service/project'),
    { updateToQiniu, returnInfo, fileUploadLegel } = require('../lib/utils'),
    { nanoid } = require('nanoid'),
    { IMAGE_UPLOAD, API } = require('../config/err_config'),
    { qiniu } = require('../config/config'),
    path = require('path');
class Project {
    async getProjectC(ctx) {
        try {
            const { id } = ctx.request.body;
            let retrunMess = await getProject(id);
            ctx.body = returnInfo(API.GET_PROJECT_SUCCESS, retrunMess);
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(API.GET_PROJECT_ERROR);
        }
    }
    async getProjectUp(ctx) {
        try {
            let retrunMess = await getProjectUp();
            ctx.body = returnInfo(API.GET_PROJECT_SUCCESS, retrunMess);
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(API.GET_PROJECT_ERROR);
        }
    }
    async createProjectC(ctx) {
        let projectimg = '';
        let isLegel = fileUploadLegel(ctx);
        if (isLegel) {
            const files = Object.entries(ctx.request.files),
                { projectname, projectlink } = ctx.request.body;
            let extname = path.extname(files[0][1].name),
                filePath = files[0][1].path,
                fileName = `projectphoto${nanoid()}${extname}`;
            try {
                updateToQiniu(filePath, fileName);
                projectimg = `${qiniu.bucket.blogsimg.domain}/${fileName}`;
            } catch (error) {
                console.log(error);
                ctx.body = IMAGE_UPLOAD.UPLOAD_ERROR;
            }
            try {
                let returnMess = await createProject({ projectlink, projectimg, projectname })
                ctx.body = returnMess ? returnInfo(API.CREATE_PROJECT_SUCESS)
                    : returnInfo(API.CREATE_PROJECT_ERROR)
            } catch (error) {
                console.log(error);
                ctx.body = returnInfo(API.CREATE_PROJECT_ERROR)
            }
        } else {
            ctx.body = returnInfo(IMAGE_UPLOAD.UPLOAD_ERROR)
            return;
        }
    }
    async destroyProjectC(ctx) {
        let { id } = ctx.request.body;
        try {
            let returnMess = await destroyProject(id);
            ctx.body = returnMess ? returnInfo(API.DESTROY_PROJECT_SUCCESS)
                : returnInfo(API.DESTROY_PROJECT_ERROR)
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(API.DESTROY_PROJECT_ERROR);
        }
    }
    async updateProjectC(ctx) {
        const files = ctx.request.files,
            { projectname, projectlink, id } = ctx.request.body;
        let projectimg = '';
        let isLegel = fileUploadLegel(ctx);
        if (isLegel) {
            const filesArr = Object.entries(files);
            let extname = path.extname(filesArr[0][1].name),
                filePath = filesArr[0][1].path,
                fileName = `projectphoto${nanoid()}${extname}`;
            try {
                updateToQiniu(filePath, fileName);
                projectimg = `${qiniu.bucket.blogsimg.domain}/${fileName}`;
            } catch (error) {
                console.log(error);
                ctx.body = IMAGE_UPLOAD.UPLOAD_ERROR;
            }
        }
        try {
            let returnMess = await updateProject(id, { projectname, projectimg, projectlink });
            ctx.body = returnMess ? returnInfo(API.UPDATE_PROJECT_SUCCESS)
                : returnInfo(API.UPDATE_PROJECT_ERROR)
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(API.UPDATE_PROJECT_ERROR)
        }
    }
    async changeStatusProjectC(ctx) {
        try {
            const { id, status } = ctx.request.body;
            let returnMess = await changeStatusProject(id, status);
            ctx.body = returnMess ? returnInfo(API.CHANGE_STATUS_PROJECT_SUCCESS)
                : returnInfo(API.CHANGE_STATUS_PROJECT_ERROR)
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(API.CHANGE_STATUS_PROJECT_ERROR)
        }

    }
}
module.exports = new Project();    